package cn.edu.cqvie.ssm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

import cn.edu.cqvie.ssm.entity.Student;
import cn.edu.cqvie.ssm.model.StudentModel;
import cn.edu.cqvie.ssm.model.impl.StudentModelImpl;

/**
 * 学生信息控制器
 * 
 * @author zhengsh
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {

	StudentModel sm = new StudentModelImpl();
	private static final long serialVersionUID = 1L;

	/**
	 * 查询学生信息
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action") == null ? "" : String.valueOf(req.getParameter("action"));
		switch (action) {
		case "insert":
			req.getRequestDispatcher("6_4/insert.jsp").forward(req, resp);
			break;
		case "update":
			setStu2Req(req);
			req.getRequestDispatcher("6_4/update.jsp").forward(req, resp);
			break;
		case "delete":
			setStu2Req(req);
			req.getRequestDispatcher("6_4/delete.jsp").forward(req, resp);
			break;
		default:
			list(req, resp);
			break;
		}

	}

	private void setStu2Req(HttpServletRequest req) {
		int id = Integer.valueOf(String.valueOf(req.getParameter("id")));
		Student stu = sm.get(id);
		req.setAttribute("stu", stu);
	}

	/**
	 * 返回列表页面
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Student> stus = sm.list(null);
		req.setAttribute("stus", stus);
		req.getRequestDispatcher("6_4/list.jsp").forward(req, resp);
	}

	/**
	 * 学生信息保存
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String action = req.getParameter("action") == null ? "" : String.valueOf(req.getParameter("action"));
		switch (action) {
		case "put":
			doPut(req, resp);
			break;
		case "delete":
			doDelete(req, resp);
			break;
		default:
			Student stu = build(req, resp);
			sm.insert(stu);
			list(req, resp);
			break;
		}
	}

	private Student build(HttpServletRequest req, HttpServletResponse resp) {
		try {
			SmartUpload smu = new SmartUpload();
			//初始化 SmartUpload 对象
			smu.initialize(getServletConfig(), req , resp);
			//定义允许上传文件类型(可选设置项)
			smu.setAllowedFilesList("gif,jpg,doc,txt,xls,PNG,jpeg");
			//不允许上传文件类型(可选设置项)
			smu.setDeniedFilesList("exe,bat");
			//单个文件最大限制单位：B(可选设置项)
			smu.setMaxFileSize(1000000);
			//总共上传文件限制(可选设置项)
			smu.setTotalMaxFileSize(20000000);
			smu.setCharset("UTF-8");
			//执行上传
			smu.upload();
			File file = null;
			file = smu.getFiles().getFile(0);
			//文件存储路径
			String filePath = "";
			//webapp目录真实路径
			String rootPath =  getServletContext().getRealPath("/");
			if (!file.isMissing()) {
				//设置文件在服务器的保存位置
				filePath = "upload/";
				//文件路径和文件名
				filePath += file.getFileName();
				//文件另存到相对路径的文件夹中
				file.saveAs(rootPath + filePath, SmartUpload.SAVE_AUTO);
			}
			
			Request request = smu.getRequest();
			
			int id = request.getParameter("id") == null ? 0 : Integer.valueOf(request.getParameter("id"));
			String name = request.getParameter("name") == null ? null : String.valueOf(request.getParameter("name"));
			String sex = request.getParameter("sex") == null ? null : String.valueOf(request.getParameter("sex"));
			int age = request.getParameter("age") == null ? 0 : Integer.valueOf(request.getParameter("age"));
			String grade = request.getParameter("grade") == null ? null : String.valueOf(request.getParameter("grade"));
			int score = request.getParameter("score") == null ? 0 : Integer.valueOf(request.getParameter("score"));
			
			Student stu = Student.builder()
					.id(id)
					.name(name)
					.sex(sex)
					.age(age)
					.grade(grade)
					.score(score)
					.photo(filePath)
					.build();
			return stu;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private Student build(HttpServletRequest req) {
		int id = req.getParameter("id") == null ? 0 : Integer.valueOf(req.getParameter("id"));
		String name = req.getParameter("name") == null ? null : String.valueOf(req.getParameter("name"));
		String sex = req.getParameter("sex") == null ? null : String.valueOf(req.getParameter("sex"));
		int age = req.getParameter("age") == null ? 0 : Integer.valueOf(req.getParameter("age"));
		String grade = req.getParameter("grade") == null ? null : String.valueOf(req.getParameter("grade"));
		int score = req.getParameter("score") == null ? 0 : Integer.valueOf(req.getParameter("score"));
		String photo = req.getParameter("photo") == null ? null : String.valueOf(req.getParameter("photo"));

		Student stu = Student.builder()
				.id(id)
				.name(name)
				.sex(sex)
				.age(age)
				.grade(grade)
				.score(score)
				.photo(photo)
				.build();
		return stu;
	}

	/**
	 * 修改学生信息
	 */
	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		Student stu = build(req,resp);
		sm.update(stu);
		list(req, resp);
	}

	/**
	 * 删除学生信息
	 */
	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Student stu = build(req);
		sm.delete(stu);
		list(req, resp);
	}

}
